package com.service.device;

import com.common.utils.DateUtils;
import com.dao.secondary.device.DeviceObdRepairExFactoryBatchInfoMapper;
import com.github.pagehelper.PageHelper;
import com.model.domain.device.GetDeviceObdBaseInfoDO;
import com.model.domain.device.GetDeviceObdBatchRepairRecordMappingInfoDO;
import com.model.domain.device.GetDeviceObdRepairExFactoryBatchInfoDO;
import com.model.pojo.device.DeviceObdBaseInfo;
import com.model.pojo.device.DeviceObdBatchRepairRecordMappingInfo;
import com.model.pojo.device.DeviceObdExFactoryBatchInfo;
import com.model.pojo.device.DeviceObdRepairExFactoryBatchInfo;
import com.model.pojo.user.UserInfo;
import com.service.user.UserInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author: zql
 * @Description: OBD设备出厂批次管理服务
 * @Data: 2023-07-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceObdRepairExFactoryBatchInfoService {
    private final DeviceObdRepairExFactoryBatchInfoMapper deviceObdRepairExFactoryBatchInfoMapper;
    private final DeviceObdBaseInfoService deviceObdBaseInfoService;
    private final DeviceObdBatchRepairRecordMappingInfoService deviceObdBatchRepairRecordMappingInfoService;
    private final UserInfoService userInfoService;

    /**
     * 分页数据
     *
     * @return List<DeviceObdExFactoryBatchInfo>
     * @author tm
     * @date 2023-07-10 22:36
     */
    public List<DeviceObdRepairExFactoryBatchInfo> getPageList(GetDeviceObdRepairExFactoryBatchInfoDO getDeviceObdRepairExFactoryBatchInfoDO) {
        PageHelper.clearPage();
        PageHelper.startPage(getDeviceObdRepairExFactoryBatchInfoDO.getPage(), getDeviceObdRepairExFactoryBatchInfoDO.getPageSize(), getDeviceObdRepairExFactoryBatchInfoDO.getSortRow() + getDeviceObdRepairExFactoryBatchInfoDO.getSortOrder());
        return deviceObdRepairExFactoryBatchInfoMapper.selectList(getDeviceObdRepairExFactoryBatchInfoDO);
    }

    public List<DeviceObdRepairExFactoryBatchInfo> getAllList(GetDeviceObdRepairExFactoryBatchInfoDO getDeviceObdRepairExFactoryBatchInfoDO) {
        PageHelper.clearPage();
        PageHelper.orderBy(getDeviceObdRepairExFactoryBatchInfoDO.getSortRow() + getDeviceObdRepairExFactoryBatchInfoDO.getSortOrder());
        return deviceObdRepairExFactoryBatchInfoMapper.selectList(getDeviceObdRepairExFactoryBatchInfoDO);
    }

    public int save(DeviceObdRepairExFactoryBatchInfo batchInfo) {
        batchInfo.setCreateTime(new Date());
        return deviceObdRepairExFactoryBatchInfoMapper.insertSelective(batchInfo);
    }

    public DeviceObdRepairExFactoryBatchInfo getById(long batchId) {
        return deviceObdRepairExFactoryBatchInfoMapper.selectByPrimaryKey(batchId);
    }

    public int update(DeviceObdRepairExFactoryBatchInfo batchInfo) {
        batchInfo.setLastModifiedTime(new Date());
        return deviceObdRepairExFactoryBatchInfoMapper.updateByPrimaryKeySelective(batchInfo);
    }

    @Transactional
    public int updateExFactory(DeviceObdRepairExFactoryBatchInfo batchInfo) {
        long factoryUserId = 0;
        int num = 0;
        boolean flag = true;
        flag = false;
        int serviceYear = batchInfo.getServiceYear();
        GetDeviceObdBatchRepairRecordMappingInfoDO getDeviceObdBatchRepairRecordMappingInfoDO = new GetDeviceObdBatchRepairRecordMappingInfoDO();
        getDeviceObdBatchRepairRecordMappingInfoDO.setBatchId(batchInfo.getBatchId());
        getDeviceObdBatchRepairRecordMappingInfoDO.setSortRow("createTime");
        List<String> dtuList = deviceObdBatchRepairRecordMappingInfoService.getAllList(getDeviceObdBatchRepairRecordMappingInfoDO).stream().map(DeviceObdBatchRepairRecordMappingInfo::getDtu).collect(Collectors.toList());
        GetDeviceObdBaseInfoDO getDeviceObdBaseInfoDO = new GetDeviceObdBaseInfoDO();
        getDeviceObdBaseInfoDO.setDtus(dtuList);
        getDeviceObdBaseInfoDO.setSortRow("id");
        List<DeviceObdBaseInfo> list = new ArrayList<>();
        List<DeviceObdBaseInfo> baseList = deviceObdBaseInfoService.getAllList(getDeviceObdBaseInfoDO);
        if (baseList != null && baseList.size() > 0) {
            for (DeviceObdBaseInfo info : baseList) {
                //如果厂家是沃得或道依茨法尔，服务时间按出厂时间来计算
                Date serviceStartTime = new Date();
                if (info.getServiceStartTime() != null) {
                    serviceStartTime = info.getServiceStartTime();
                }
                Date serviceEndTime = DateUtils.addDate(serviceStartTime, serviceYear, 0, 0, 0, 0, 0, 0);
                info.setServiceStartTime(serviceStartTime);
                info.setServiceEndTime(serviceEndTime);
                info.setLastModifiedTime(new Date());
                list.add(info);
            }
            deviceObdBaseInfoService.updateList(list);
            flag = true;
        }
        if (flag) {
            batchInfo.setLastModifiedTime(new Date());
            num = deviceObdRepairExFactoryBatchInfoMapper.updateByPrimaryKeySelective(batchInfo);
        }
        return num;
    }
}
